﻿using DLToolkit.Forms.Controls;
using Moven.ViewModels;
using System.Linq;
using Xamarin.Forms;

namespace Moven.Views
{
    class TagsPage: BasePage
    {
        public TagsPage()
        {
            BindingContext = new TagsViewModel();
            var tagEntryView = new TagEntryView
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand,
                TagViewFactory = () => new TagItemView(),
                Spacing = 8
            };

            tagEntryView.SetBinding<TagsViewModel>(TagEntryView.TagItemsProperty, v => v.Items);

            tagEntryView.TagTapped += (sender, e) =>
            {
                var item = (TagsViewModel.TagItem) e.Item;
                var tag = item.Name;
                var articles = Statics.ArticlesStore.
                    GetArticlesAsync().Result.Articles.
                    Where(x => x.Tags.Contains(tag)).ToArray();
                Navigation.PushAsync(new ArticlesPage(tag + "墨文", articles, MenuType.ArticlesOfTag) { Tag = tag });
            };
            Content = new ScrollView
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand,
                Padding = 10,
                Content = tagEntryView
            };

			this.SetBackButtonTitle();
        }

        class TagItemView : Frame
        {
            public TagItemView()
            {
                BackgroundColor = Color.FromHex("#2196F3");
                OutlineColor = Color.Transparent;
                Padding = 10;

                var label = new Label();
                label.SetBinding<TagsViewModel.TagItem>(Label.TextProperty, v => v.Text);

                Content = label;
            }
        }
    }
}
